package ink.xiaobaibai.serviceOxy.monthly.impl;

import ink.xiaobaibai.common.delay.DeviceDelayItem;
import ink.xiaobaibai.common.delay.DeviceDelayItemAddOrderId;
import ink.xiaobaibai.common.delay.MyDelayQueue;
import ink.xiaobaibai.serviceOxy.monthly.IDelayMonControlService;
import ink.xiaobaibai.serviceOxy.monthly.IDeviceMonControlService;
import ink.xiaobaibai.serviceOxy.monthly.IOrderMonService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @description: 月费的延时队列
 * @author: 小白白
 * @create: 2021-05-31
 **/

@Service
public class DelayMonControlServiceImpl implements IDelayMonControlService, ApplicationRunner {

    @Autowired
    private IOrderMonService orderMonService;

    @Autowired
    private IDeviceMonControlService deviceMonControlService;

    // 已经ShutdownProtectionService保护
    public MyDelayQueue<DeviceDelayItemAddOrderId> delayQueue = new MyDelayQueue<>();

    @Override
    public void autoActivation(String orderId) {
        DeviceDelayItemAddOrderId deviceDelayItem =
                new DeviceDelayItemAddOrderId(null, DateUtils.addDays(new Date(), 7), orderId);
        this.delayQueue.add(deviceDelayItem);
    }

    @Override
    public void removeActivation(String orderId) {
        this.delayQueue.remove(new DeviceDelayItemAddOrderId(null, null, orderId));
    }

    @Override
    public void closeDevice(Integer deviceId, Integer seconds) {
        DeviceDelayItem deviceDelayItem = new DeviceDelayItem(deviceId, DateUtils.addSeconds(new Date(), seconds));
        this.delayQueue.add(deviceDelayItem);
    }

    @Override
    public void removeDevice(Integer deviceId) {
        this.delayQueue.remove(new DeviceDelayItem(deviceId, null));
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.execute(() -> {
            while (true) {
                try {
                    DeviceDelayItem item = this.delayQueue.take();
                    if (item instanceof DeviceDelayItemAddOrderId) {
                        //延时的
                        DeviceDelayItemAddOrderId itemAddOrderId = (DeviceDelayItemAddOrderId) item;
                        //查询其是否变为激活状态,如果不是激活状态,就改为激活状态
                        String orderId = itemAddOrderId.getOrderId();
                        this.orderMonService.activateDevice(orderId);
                    } else {
                        //机器的,需要关闭
                        this.deviceMonControlService.closeDeviceAtOnce(item.getDeviceId());
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
